package gr.handmademarket.ui.panels;

import gr.handmademarket.po.implementations.Product;
import gr.handmademarket.po.implementations.Seller;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.panel.EmptyPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;

public class SellerProductsPanel 
extends Panel{

	/**
	 * serialVersionUID.
	 */
	private static final long serialVersionUID = 1L; 
	
	private Seller seller;
	
	private Panel productPanel = new EmptyPanel("productPanel");
	
	@SuppressWarnings("rawtypes")
	private Form<?> form = new Form("form");
	
	private AjaxButton newBtn = new AjaxButton("newBtn") {
		/**
		 * serialVersionUID
		 */
		private static final long serialVersionUID = 1L;
		
		@Override
		protected void onSubmit(AjaxRequestTarget target, Form<?> form1) {
			Product p = new Product();
			p.setSeller(seller);
			callback(target, p);
		}
	};
		
	public SellerProductsPanel(String id, IModel<Seller> model) {
		super(id, model);
		seller = model.getObject();
		ProductsInfoPanel listPanel = new ProductsInfoPanel("productsList", seller,this);
		add(listPanel,productPanel,form);
		productPanel.setOutputMarkupPlaceholderTag(true);
		form.add(newBtn);
	}
	
	public void callback(AjaxRequestTarget target, Product product){
		UploadProductPanel newPanel = new UploadProductPanel("productPanel", new CompoundPropertyModel<Product>(product));
		this.get("productPanel").replaceWith(newPanel);
		productPanel = newPanel;
		target.add(productPanel);
		productPanel.setOutputMarkupPlaceholderTag(true);
	}

}
